package com.intexh.quickandroid.module.chat.util;

import android.os.AsyncTask;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Pattern;

/**
 * Created by showu on 13-7-5.
 */
public class FileDownload extends AsyncTask<Object, Object, File> {
    private final static Pattern p = Pattern.compile("^http.+");
    private IDownLoadListener downLoadListener;
    private File file;
    private static final String TAG = "FileDownload";

    public FileDownload(IDownLoadListener downLoadListener, File file) {
        if (downLoadListener != null)
            this.downLoadListener = downLoadListener;
       /*
        *  this.file = file;
        *  2014.11.12
        *  leo
        */
        if(file != null){
        	this.file = file;
        }else{
        	return;
        }
    }

    @Override
    protected File doInBackground(Object... params) {
        if(params[0] == null) return null;
        String httpUrl = params[0].toString();
        if (httpUrl == null || !p.matcher(httpUrl.trim()).matches()) {
            return null;
        }
        return download(params[0].toString());
    }

    @Override
    protected void onPostExecute(File file) {
        if (downLoadListener != null&&file!=null)
            downLoadListener.downLoadEnd(file);
    }


    private File download(String httpUrl) {
//        LogUtil.e(TAG, "GET=>" + httpUrl);
        InputStream is = null;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            int response = conn.getResponseCode();
            if (response == 200) {
                if (downLoadListener != null) {
                    downLoadListener.startDownload(conn.getContentLength());
                }
                is = conn.getInputStream();
                //* 2014-11-29下午2:25:36 Begin edited by YuSuCheng */
                if(file!=null){
                FileOutputStream outputStream = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    if (downLoadListener != null) {
                        downLoadListener.downloading(len);
                    }
                    outputStream.write(buffer);
                }
                outputStream.flush();
                outputStream.close();
                is.close();
                return file;
                }
                /* 2014-11-29下午2:25:45 End edited by Yusucheng */
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        file.delete();
        if (downLoadListener != null) downLoadListener.downLoadError();
        return null;

    }

}
